package com.jml.springsecurity.validate.code;

import com.jml.springsecurity.core.properties.SecurityProperties;
import com.jml.springsecurity.validate.code.image.ImageCodeGenerator;
import com.jml.springsecurity.validate.code.sms.DefaultSmsCodeSender;
import com.jml.springsecurity.validate.code.sms.SmsCodeGenerator;
import com.jml.springsecurity.validate.code.sms.SmsCodeSender;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;


@Configuration
public class ValidateCodeBeanConfig {
	@Autowired
	private SecurityProperties securityProperties;
	
	@Bean //这个注解是启动服务的时候初始化这个bean之前会在spring容器中找一下是否一个名字为这个的bean，如果找到了就用找到的，如果没有就会创建
	@ConditionalOnMissingBean(name = "imageCodeGenerator")
	public ValidateCodeGenerator imageValidateCodeGenerator() {
		ImageCodeGenerator imageCodeGenerator = new ImageCodeGenerator();
		imageCodeGenerator.setSecurityProperties(securityProperties);
		return imageCodeGenerator;
	}

	@Bean //这个注解是启动服务的时候初始化这个bean之前会在spring容器中找一下是否一个名字为这个的bean，如果找到了就用找到的，如果没有就会创建
	@ConditionalOnMissingBean(SmsCodeGenerator.class)
	public ValidateCodeGenerator smsValidateCodeGenerator() {
		SmsCodeGenerator codeGenerator = new SmsCodeGenerator();
		codeGenerator.setSecurityProperties(securityProperties);
		return codeGenerator;
	}

	@Bean
	@ConditionalOnMissingBean(SmsCodeSender.class)
	public SmsCodeSender smsCodeSender() {
		return new DefaultSmsCodeSender();
	}
}
